package a630wj.recycleview630wj.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import a630wj.recycleview630wj.R;
import a630wj.recycleview630wj.adapter.ArticleAdapter;
import a630wj.recycleview630wj.bean.ArticleBean;
import a630wj.recycleview630wj.iface.ArticleListener;
import a630wj.recycleview630wj.model.ArticleModel;

/**
 * Created by weijie on 2017/6/6.
 */

public class ArticleFragment extends Fragment{
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ArticleAdapter articleAdapter;
    private List<ArticleBean> list=null;
    private View view=null;
    private Context context;
    private static int cur_page=1;
    private static int lastVisibleItemPosition;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.artiiclefragment,container,false);
        context=view.getContext();

        initRecyclerView();
        ArticleModel articleModel =new ArticleModel();
        articleModel.getResultList("article",2,"8soafn3tpaj0efuab5jvmpbhr4",articleListener);

        return view;
    }


    ArticleListener articleListener =new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> listbean) {

              list=listbean;
            articleAdapter.setList(list);

     /*     if(cur_page==1){
              list=listbean;
              articleAdapter.setList(list);

          }else{
              list.removeAll(listbean);

              list.addAll(0,listbean);
              articleAdapter.setList(list);
          }*/

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"fail:"+msg,Toast.LENGTH_SHORT).show();
        }
    };


    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.rv_article);

        //创建默认的线性LayoutManager
        layoutManager =new LinearLayoutManager(context);
        //设置布局
        recyclerView.setLayoutManager(layoutManager);
        //设置item的高度
        recyclerView.setHasFixedSize(true);
        //创建适配器
//        list=getData();
        articleAdapter=new ArticleAdapter(context);
 /*       ArticleAdapter.OnItemClickListener onItemClickListener= new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), ArticleDetailActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                startActivity(intent);
            }
        };
       */
        articleAdapter.setList(list);

        recyclerView.setAdapter(articleAdapter);
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
//                    cur_page+=1;
//
//                    new ArticleModel().getResultList("article",cur_page,"8soafn3tpaj0efuab5jvmpbhr4",articleListener);
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                lastVisibleItemPosition=list.size()-1;
//            }
//        });
    }
    }

